package ace.cmp.spring.data.jdbc.querydsl.core.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.spring.data.jdbc.querydsl.core.LogicDeleteHelper;
import ace.cmp.spring.data.jdbc.querydsl.core.QuerydslLogicNotDeleteFlagValueGetter;
import ace.cmp.spring.data.jdbc.querydsl.core.annotations.LogicDelete;
import java.lang.reflect.Field;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/8/25 9:51
 */
@Slf4j
@AllArgsConstructor
@Component
public class QuerydslLogicNotDeleteFlagValueGetterImpl
    implements QuerydslLogicNotDeleteFlagValueGetter {
  private final LogicDeleteHelper logicDeleteHelper;

  @Override
  public Object get(Class entityType, LogicDelete logicDeleteAnnotation) {
    Field field = this.logicDeleteHelper.getLogicDeleteField(entityType);
    if (field == null) {
      return CoreConstant.DELETE_FLAG_NOT;
    }
    if (field.getType().equals(Integer.class)) {
      return CoreConstant.DELETE_FLAG_NOT.intValue();
    }
    if (field.getType().equals(Long.class)) {
      return CoreConstant.DELETE_FLAG_NOT;
    }
    if (field.getType().equals(String.class)) {
      return CoreConstant.DELETE_FLAG_NOT.toString();
    }
    return CoreConstant.DELETE_FLAG_NOT;
  }
}
